1759D - Make It Round - CodeForces Solution


brute force number theory *1400

Please click on ads to support us..

Python Code:

t=int(input())
while t:
    t-=1
    n,m=map(int,input().split())
    ans=n
    k=1
    while n>0 and n%10==0:
        n//=10
    while n>0 and n%5==0:
        n//=5
        if k*2<=m:
            k*=2
        else:
            break
    while n>0 and n%2==0:
        n//=2
        if k*5<=m:
            k*=5
        else:
            break
    while k*10<=m:
        k*=10
    k=(m//k)*k
    print(ans*k)

C++ Code:

#include<bits/stdc++.h>
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define endl "\n"

#define fastIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define fileIO freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);

void Goal() {
	long long n, m;
    cin >> n >> m;

    long long n2 = n, k = 1;;
    while(n % 10 == 0) {
        n /= 10;
    }
    while(n % 2 == 0) {
        n /= 2;    
        if((k * 5) <= m) k = k * 5;
        else break;
    }
    while(n % 5 == 0) {
        n = n / 5;
        if((k * 2 <= m)) k = k * 2;
        else break;
    }
    while((k * 10) <= m) {
        k *= 10;
    }

    k = (m / k) * k;

    long long ans = n2 * k;

    cout << ans << '\n';
}

int32_t main() {
 
    fastIO;
    #ifndef ONLINE_JUDGE
        fileIO;
    #endif

    int t;
    cin >> t;
    while(t--) {
        Goal();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

265A - Colorful Stones (Simplified Edition)
296A - Yaroslav and Permutations
967B - Watering System
152A - Marks
1398A - Bad Triangle
137A - Postcards and photos
1674D - A-B-C Sort
334A - Candy Bags
855A - Tom Riddle's Diary
1417A - Copy-paste
1038A - Equality
1061A - Coins
1676E - Eating Queries
1447A - Add Candies
1721D - Maximum AND
363C - Fixing Typos
1401A - Distance and Axis
658A - Bear and Reverse Radewoosh
1721E - Prefix Function Queries
977E - Cyclic Components
1140D - Minimum Triangulation
75C - Modified GCD
1722A - Spell Check
1722B - Colourblindness
1722D - Line
1722C - Word Game
1722G - Even-Odd XOR
552E - Vanya and Brackets
933A - A Twisty Movement
1722F - L-shapes